package router

import (
	"github.com/LyricTian/gin-admin/v7/internal/app/middleware"
	"github.com/gin-gonic/gin"
)

// RegisterAPI register api group router
func (a *Router) RegisterAPI(app *gin.Engine) {
	g := app.Group("/api")

	g.Use(middleware.UserAuthMiddleware(a.Auth,
		middleware.AllowPathPrefixSkipper("/api/v1/pub/login"),
		middleware.AllowPathPrefixSkipper("/api/v1/sets.public"),
		middleware.AllowPathPrefixSkipper("/api/v1/pays"),
	))

	g.Use(middleware.CasbinMiddleware(a.CasbinEnforcer,
		middleware.AllowPathPrefixSkipper("/api/v1/pub"),
		middleware.AllowPathPrefixSkipper("/api/v1/sets.public"),
		middleware.AllowPathPrefixSkipper("/api/v1/pays"),
	))

	g.Use(middleware.RateLimiterMiddleware())

	v1 := g.Group("/v1")
	{
		pub := v1.Group("/pub")
		{
			gLogin := pub.Group("login")
			{
				gLogin.GET("captchaid", a.LoginAPI.GetCaptcha)
				gLogin.GET("captcha", a.LoginAPI.ResCaptcha)
				gLogin.POST("", a.LoginAPI.Login)
				gLogin.POST("exit", a.LoginAPI.Logout)
			}

			gCurrent := pub.Group("current")
			{
				gCurrent.PUT("password", a.LoginAPI.UpdatePassword)
				gCurrent.GET("user", a.LoginAPI.GetUserInfo)
				gCurrent.GET("menutree", a.LoginAPI.QueryUserMenuTree)
			}
			pub.POST("/refresh-token", a.LoginAPI.RefreshToken)
		}

		gDemo := v1.Group("demos")
		{
			gDemo.GET("", a.DemoAPI.Query)
			gDemo.GET(":id", a.DemoAPI.Get)
			gDemo.POST("", a.DemoAPI.Create)
			gDemo.PUT(":id", a.DemoAPI.Update)
			gDemo.DELETE(":id", a.DemoAPI.Delete)
			gDemo.PATCH(":id/enable", a.DemoAPI.Enable)
			gDemo.PATCH(":id/disable", a.DemoAPI.Disable)
		}

		gMenu := v1.Group("menus")
		{
			gMenu.GET("", a.MenuAPI.Query)
			gMenu.GET(":id", a.MenuAPI.Get)
			gMenu.POST("", a.MenuAPI.Create)
			gMenu.PUT(":id", a.MenuAPI.Update)
			gMenu.DELETE(":id", a.MenuAPI.Delete)
			gMenu.PATCH(":id/enable", a.MenuAPI.Enable)
			gMenu.PATCH(":id/disable", a.MenuAPI.Disable)
		}
		v1.GET("/menus.tree", a.MenuAPI.QueryTree)

		gRole := v1.Group("roles")
		{
			gRole.GET("", a.RoleAPI.Query)
			gRole.GET(":id", a.RoleAPI.Get)
			gRole.POST("", a.RoleAPI.Create)
			gRole.PUT(":id", a.RoleAPI.Update)
			gRole.DELETE(":id", a.RoleAPI.Delete)
			gRole.PATCH(":id/enable", a.RoleAPI.Enable)
			gRole.PATCH(":id/disable", a.RoleAPI.Disable)
		}
		v1.GET("/roles.select", a.RoleAPI.QuerySelect)

		gUser := v1.Group("users")
		{
			gUser.GET("", a.UserAPI.Query)
			gUser.GET(":id", a.UserAPI.Get)
			gUser.POST("", a.UserAPI.Create)
			gUser.PUT(":id", a.UserAPI.Update)
			gUser.DELETE(":id", a.UserAPI.Delete)
			gUser.PATCH(":id/enable", a.UserAPI.Enable)
			gUser.PATCH(":id/disable", a.UserAPI.Disable)
		}

		// 注册/api/v1/sets
		gSet := v1.Group("sets")
		{
			gSet.GET("", a.SetAPI.Query)
			gSet.GET(":id", a.SetAPI.Get)
			gSet.POST("", a.SetAPI.Create)
			gSet.PUT(":id", a.SetAPI.Update)
			gSet.DELETE(":id", a.SetAPI.Delete)
			gSet.PATCH(":id/enable", a.SetAPI.Enable)
			gSet.PATCH(":id/disable", a.SetAPI.Disable)
		}
		v1.GET("sets.public", a.SetAPI.Public)
		v1.POST("/upload_file", a.SetAPI.UploadFile)

		// 注册/api/v1/group_homes
		gGroupHome := v1.Group("group_homes")
		{
			gGroupHome.GET("", a.GroupHomeAPI.Query)
			gGroupHome.GET(":id", a.GroupHomeAPI.Get)
			gGroupHome.POST("", a.GroupHomeAPI.Create)
			gGroupHome.PUT(":id", a.GroupHomeAPI.Update)
			gGroupHome.DELETE(":id", a.GroupHomeAPI.Delete)
		}

		// 注册/api/v1/group_videos
		gGroupVideo := v1.Group("group_videos")
		{
			gGroupVideo.GET("", a.GroupVideoAPI.Query)
			gGroupVideo.GET(":id", a.GroupVideoAPI.Get)
			gGroupVideo.POST("", a.GroupVideoAPI.Create)
			gGroupVideo.PUT(":id", a.GroupVideoAPI.Update)
			gGroupVideo.DELETE(":id", a.GroupVideoAPI.Delete)
		}
		// 注册/api/v1/likes
		gLike := v1.Group("likes")
		{
			gLike.GET("", a.LikeAPI.Query)
			gLike.GET(":id", a.LikeAPI.Get)
			gLike.POST("", a.LikeAPI.Create)
			gLike.PUT(":id", a.LikeAPI.Update)
			gLike.DELETE(":id", a.LikeAPI.Delete)
		}
		// 注册/api/v1/orders
		gOrder := v1.Group("orders")
		{
			gOrder.GET("", a.OrderAPI.Query)
			gOrder.GET(":id", a.OrderAPI.Get)
			gOrder.POST("", a.OrderAPI.Create)
			gOrder.PUT(":id", a.OrderAPI.Update)
			gOrder.DELETE(":id", a.OrderAPI.Delete)
			gOrder.PATCH(":id/enable", a.OrderAPI.Enable)
			gOrder.PATCH(":id/disable", a.OrderAPI.Disable)
		}
		//支付路由
		pay := v1.Group("pays")
		{
			wechat := pay.Group("wechat")
			{
				//微信h5支付
				wechat.GET("h5", a.OrderAPI.PayH5)
				wechat.GET("weapp", a.OrderAPI.PayWeApp)
				wechat.GET("weapp.login", a.OrderAPI.Login)
				wechat.GET("weapp.accesstoken", a.OrderAPI.GetAccessToken)
				wechat.POST("notify", a.OrderAPI.WeAppPayNotify)
			}
		}
		// 注册/api/v1/user_infos
		gUserInfo := v1.Group("user_infos")
		{
			gUserInfo.GET("", a.UserInfoAPI.Query)
			gUserInfo.GET(":id", a.UserInfoAPI.Get)
			gUserInfo.POST("", a.UserInfoAPI.Create)
			gUserInfo.PUT(":id", a.UserInfoAPI.Update)
			gUserInfo.DELETE(":id", a.UserInfoAPI.Delete)
			gUserInfo.PATCH(":id/enable", a.UserInfoAPI.Enable)
			gUserInfo.PATCH(":id/disable", a.UserInfoAPI.Disable)
		}
		//设置邀请码
		v1.GET("user_infos.account_num",a.UserInfoAPI.SetAccountNum)
		// 注册/api/v1/user_logins
		gUserLogin := v1.Group("user_logins")
		{
			gUserLogin.GET("", a.UserLoginAPI.Query)
			gUserLogin.GET(":id", a.UserLoginAPI.Get)
			gUserLogin.POST("", a.UserLoginAPI.Create)
			gUserLogin.PUT(":id", a.UserLoginAPI.Update)
			gUserLogin.DELETE(":id", a.UserLoginAPI.Delete)
			gUserLogin.PATCH(":id/enable", a.UserLoginAPI.Enable)
			gUserLogin.PATCH(":id/disable", a.UserLoginAPI.Disable)
		}

		// 注册/api/v1/group_notifications
		gGroupNotification := v1.Group("group_notifications")
		{
			gGroupNotification.GET("", a.GroupNotificationAPI.Query)
			gGroupNotification.GET(":id", a.GroupNotificationAPI.Get)
			gGroupNotification.POST("", a.GroupNotificationAPI.Create)
			gGroupNotification.PUT(":id", a.GroupNotificationAPI.Update)
			gGroupNotification.DELETE(":id", a.GroupNotificationAPI.Delete)
		}
		// 注册/api/v1/groups
		gGroup := v1.Group("groups")
		{
			gGroup.GET("", a.GroupAPI.Query)
			gGroup.GET(":id", a.GroupAPI.Get)
			gGroup.POST("", a.GroupAPI.Create)
			gGroup.PUT(":id", a.GroupAPI.Update)
			gGroup.DELETE(":id", a.GroupAPI.Delete)
			gGroup.PATCH(":id/enable", a.GroupAPI.Enable)
			gGroup.PATCH(":id/disable", a.GroupAPI.Disable)
		}
		// 注册/api/v1/group_members
		gGroupMember := v1.Group("group_members")
		{
			gGroupMember.GET("", a.GroupMemberAPI.Query)
			gGroupMember.GET(":id", a.GroupMemberAPI.Get)
			gGroupMember.POST("", a.GroupMemberAPI.Create)
			gGroupMember.PUT(":id", a.GroupMemberAPI.Update)
			gGroupMember.DELETE(":id", a.GroupMemberAPI.Delete)
			gGroupMember.PATCH(":id/enable", a.GroupMemberAPI.Enable)
			gGroupMember.PATCH(":id/disable", a.GroupMemberAPI.Disable)
		}
		// 注册/api/v1/videos
		gVideo := v1.Group("videos")
		{
			gVideo.GET("", a.VideoAPI.Query)
			gVideo.GET(":id", a.VideoAPI.Get)
			gVideo.POST("", a.VideoAPI.Create)
			gVideo.PUT(":id", a.VideoAPI.Update)
			gVideo.DELETE(":id", a.VideoAPI.Delete)
			gVideo.PATCH(":id/enable", a.VideoAPI.Enable)
			gVideo.PATCH(":id/disable", a.VideoAPI.Disable)
		}
	}
}
